/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.suggest;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.user.client.ui.*;

import finbudget.client.general.NodeSuggestionOracle;
import finbudgettable.client.ui.grid.Utils;
import finbudgettable.client.ui.grid.column.FilterPanel;
import finbudgettable.client.ui.grid.filter.Filter;
import finbudgettable.client.ui.grid.filter.FilterChangeEvent;
import finbudgettable.client.ui.grid.filter.FilterChangeHandler;

public class SuggestFilterPanel extends VerticalPanel implements FilterPanel {

    interface Strings extends Messages {
        @DefaultMessage("Фильтр (допускает несколько значений через запятую)")
        String filter();

        @DefaultMessage("Значение включает")
        String value();

        @DefaultMessage("Установить")
        String applyFilter();

        @DefaultMessage("Содержит {0}")
        String contains(String value);
    }

    static final Strings STRINGS = GWT.create(Strings.class);
    private final TextBox textBox;
    private final String fieldName;
    private final NodeSuggestionOracle oracle;


    public SuggestFilterPanel(String fieldName, NodeSuggestionOracle oracle) {
        this.fieldName = fieldName;
        this.oracle = oracle;
        Label filterLabel = new Label(STRINGS.filter());

        Label valueLabel = new Label(STRINGS.value());

        textBox = new TextBox();

        final Button applyButton = new Button(STRINGS.applyFilter());
        applyButton.setStyleName(Utils.blueButton);
        applyButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                String value = getStringValue();
                fireEvent(new FilterChangeEvent(
                        new ContainSuggestFilter(
                                SuggestFilterPanel.this.fieldName, value, SuggestFilterPanel.this.oracle)));
            }
        });

        textBox.addKeyDownHandler(new KeyDownHandler() {
            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    applyButton.click();
                }
            }
        });

        FlexTable table = new FlexTable();
        table.setStyleName(SuggestCell.RESOURCES.style().bSuggestFilterPanel());
        table.setWidget(0, 0, filterLabel);
        table.getFlexCellFormatter().setColSpan(0, 0, 2);
        table.setWidget(1, 0, valueLabel);
        table.setWidget(1, 1, textBox);
        table.setWidget(2, 1, applyButton);
        add(table);

//        Grid grid = new Grid(3, 2);
//        grid.setStyleName(SuggestCell.RESOURCES.style().bSuggestFilterPanel());
//        grid.setWidget(0, 0, filterLabel);
//        grid.setWidget(1, 0, valueLabel);
//        grid.setWidget(1, 1, textBox);
//        grid.setWidget(2, 1, applyButton);
//
//        add(grid);
    }

    private String getStringValue() {
        return textBox.getValue();
    }


    @Override
    public void setFilter(Filter filter) {
        if (filter instanceof ContainSuggestFilter) {
            textBox.setText(((ContainSuggestFilter) filter).getValue());
            return;
        }
        textBox.setText("");
    }

    @Override
    public void addFilterChangeHandler(FilterChangeHandler handler) {
        addHandler(handler, FilterChangeEvent.TYPE);
    }
}
